package edu.upenn.cis542;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.ToggleButton;




public class TempSensorActivity extends Activity {
	 ToggleButton toggle;
	// NetworkTask networktask;
	 Button click,clicknext;
	 RadioGroup radiogrp;
	 RadioButton myOption1, myOption2;
	 private boolean flag ;
	 private boolean connected = false;
	  private Handler handler = new Handler();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myOption1 = (RadioButton)findViewById(R.id.temp2);
        myOption2 = (RadioButton)findViewById(R.id.temp1);
        myOption1.setOnClickListener(myOptionOnClickListener);
        myOption2.setOnClickListener(myOptionOnClickListener);
        myOption1.setChecked(true);
        click = (Button)findViewById(R.id.click);
      //  clicknext = (Button)findViewById(R.id.clicknext);
        click.setOnClickListener(connectListener);
       // clicknext.setEnabled(false);
        clicknext.setOnClickListener(connectNextListener);
      
        //networktask = new NetworkTask();
    }
    
    RadioButton.OnClickListener myOptionOnClickListener =
    		   new RadioButton.OnClickListener()
    		  {
    		  public void onClick(View v) {
               HandleButtons();
               } 
    		  };
    
      private OnClickListener connectListener = new OnClickListener() {

    		        public void onClick(View v) {
    		                 flag=false;
    		            	click.setEnabled(false);
    		            	clicknext.setEnabled(true);
    		               // TextView t = (TextView)findViewById(R.id.tv2); 	
    		              //  t.setText("ON"); 
    		                    Thread cThread = new Thread(new ClientThread());
    		                    cThread.start();
    		                   // connected=false;
    		            }
    		        
    		    };
    		    
    		    private OnClickListener connectNextListener = new OnClickListener() {

    		        public void onClick(View v) {
    		                  flag=true;
    		            	clicknext.setEnabled(false);
    		            	click.setEnabled(true);
    		            	//flag=true;
    		              //  TextView t = (TextView)findViewById(R.id.tv2); 	
    		             //   t.setText("OFF"); 
    		                    Thread cThread2 = new Thread(new ClientThread());
    		                    cThread2.start();
    		                   
    		                   // connected=false;
    		            }
    		        
    		    };

    		    public class ClientThread implements Runnable {

    		        public void run() {
    		            try {
    		                Log.d("ClientActivity", "C: Connecting...");
    		                Socket socket = new Socket("10.0.2.2", 1234);
    		                connected = true;
    		                while (connected) {
    		                    try {
    		                        Log.d("ClientActivity", "C: Sending command.");
    		                       // out = new PrintWriter(echoSocket.getOutputStream(), true);
    		                        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
    		                        if (flag==false)
    		                        {
    		                            out.println("ON");
    		                           //flag=true;
    		                        }
    		                        else if (flag==true)
    		                        {
    		                        	out.println("OFF");
    		                           // flag = false;
    		                        }
    		                            out.flush();
    		                            Log.d("ClientActivity", "C: Sent.");
    		                    } catch (Exception e) {
    		                        Log.e("ClientActivity", "S: Error", e);
    		                    }
    		                }
    		                socket.close();
    		               // connected=false;
    		                Log.d("ClientActivity", "C: Closed.");
    		            } catch (Exception e) {
    		                Log.e("ClientActivity", "C: Error", e);
    		                connected = false;
    		            }
    		            
    		            
    		        }
    		    }
    		


    
    	
    public void onExitClick(View view)
    {
   	 finish();
    }
    
    
    public void HandleButtons(){
    	 if (myOption1.isChecked() )
		  {
    		 TextView t = (TextView)findViewById(R.id.temp); 	
    		 int temp  = Integer.parseInt(t.getText().toString());
    		 int conv= (temp-32)*5/9 ;
             t.setText(String.valueOf(conv)); 
             myOption1.setEnabled(false);
             myOption2.setEnabled(true);
	  }
		  else 
		  {
			  if (myOption2.isChecked())
			  {
				  TextView t = (TextView)findViewById(R.id.temp); 	
		    		 int temp  = Integer.parseInt(t.getText().toString());
		    		 int conv= temp * 9/5 + 32 ;
		             t.setText(String.valueOf(conv)); 
		             myOption2.setEnabled(false);
		             myOption1.setEnabled(true);
			  }
    }
    
    }
}
      
